package com.xinyue.game.fish.handlers;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.mygame.db.entity.Player;
import com.mygame.db.entity.model.PlayerBasic;
import com.xinyue.game.fish.FishServerErrorCode;
import com.xinyue.game.fish.messages.GetPlayerMessageRequest;
import com.xinyue.game.fish.messages.GetPlayerMessageResponse;
import com.xinyue.game.fish.messages.GetRankMessageRequest;
import com.xinyue.game.fish.messages.GetRankMessageResponse;
import com.xinyue.game.fish.messages.body.GetRankResponseBody;
import com.xinyue.game.fish.service.PlayerService;
import com.xinyue.game.fish.service.rank.EnumRankType;
import com.xinyue.game.fish.service.rank.RankInfo;
import com.xinyue.game.fish.service.rank.RankService;
import com.xinyue.game.logic.frame.GameServerContext;
import com.xinyue.network.message.handler.AbstractGameMessageHandler;
import com.xinyue.network.message.handler.GameMessageHandler;
import com.xinyue.network.message.handler.GameRequestMapping;

@GameMessageHandler
public class LoginHandler extends AbstractGameMessageHandler {

	@Autowired
	private PlayerService playerService;
	@Autowired
	private RankService rankService;

	@GameRequestMapping(GetPlayerMessageRequest.class)
	public void getPlayer(GetPlayerMessageRequest request, GameServerContext ctx) {
		long playerId = request.getHeader().getPlayerId();
		Optional<Player> playerOp = playerService.getPlayer(playerId);
		GetPlayerMessageResponse response = request.createCouple();
		if (playerOp.isPresent()) {
			Player player = playerOp.get();
			this.setInitValue(player);
			player.setNewCreate(false);
			playerService.updatePlayerBasic(player);
			playerService.updatePlayer(player);
			String playerJson = JSON.toJSONString(player);
			response.getBodyObj().setPlayer(playerJson);
		} else {
			response.header().errorCode(FishServerErrorCode.PLAYER_NOT_EXIST);
		}
		ctx.sendToGateway(response);

	}

	@GameRequestMapping(GetRankMessageRequest.class)
	public void getRank(GetRankMessageRequest request, GameServerContext ctx) {
		long playerId = request.getHeader().getPlayerId();
		int type = request.getBodyObj().getRankType();
		EnumRankType rankType = EnumRankType.getRankType(type);
		GetRankMessageResponse response = request.createCouple();
		if (rankType == null) {
			response.getHeader().errorCode(FishServerErrorCode.RANK_TYLE_ERROR);
		} else {
			int myRank = rankService.getMyRank(playerId, rankType);
			response.getBodyObj().setMyRank(myRank);
			Set<RankInfo> rankInfos = rankService.getRankInfos(rankType, 100);
			List<GetRankResponseBody.RankInfo> ranList = new ArrayList<>(rankInfos.size());
			PlayerBasic playerBasic = null;
			for (RankInfo rankInfo : rankInfos) {
				GetRankResponseBody.RankInfo voRankInfo = new GetRankResponseBody.RankInfo();
				voRankInfo.setRank(rankInfo.getRank());
				playerBasic = playerService.getPlayerBasic(Long.parseLong(rankInfo.getMember()));
				if (playerBasic != null) {
					voRankInfo.setImgUrl(playerBasic.getHeadImg());
					voRankInfo.setNickName(playerBasic.getNickName());
					voRankInfo.setValue((int) rankInfo.getValue());
					ranList.add(voRankInfo);
				}
			}
			response.getBodyObj().setRankInfos(ranList);
		}
		ctx.sendToGateway(response);
	}

	private void setInitValue(Player player) {

		if (player.getLevel() == 0) {
			player.setLevel(1);
		}
		if (player.getGold() == 0) {
			player.setGold(100);
		}
		if (player.getBullet() == 0) {
			player.setBullet(20);
		}
	}

}
